﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DAL;
using MyBind;

public partial class Communicate_bbs : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.GridView1.DataKeyNames = new string[] {"id"};
        if(!IsPostBack)
        {
            bind();
        }
    }

    //数据绑定
    //需要获得大类信息，版块信息，版主信息，版块最新主题的信息
    protected void bind()
    {
        //大类
        DAL.bbsCatalog dal = new DAL.bbsCatalog(); ;
        DataTable dt = dal.GetHasSectionList().Tables[0];
        this.GridView1.DataSource = dt.DefaultView;
        GridView1.DataBind();

        //根据类编号获得旗下版块
        for (int i = 0; i < dt.Rows.Count;i++ )
        {
            DAL.bbsSection dalSec = new bbsSection();
            DataTable dtSec = dalSec.GetDetailedList("c.id=" + dt.Rows[i]["id"].ToString()).Tables[0];
            GridView dvSec = ((GridView)GridView1.Rows[i].FindControl("GridView2"));
            dvSec.DataSource = dtSec.DefaultView;
            dvSec.DataBind();
            //显示关于每个版块其他的信息
            for (int j = 0; j < dtSec.Rows.Count;j++ )
            {
                //版主信息
                string secId=dvSec.DataKeys[j].Value.ToString();
                ((Label)dvSec.Rows[j].FindControl("lblSecMst")).Text = getSecMst(secId);
                //版块最新主题的信息
                DAL.bbsTopic dalTop = new bbsTopic();
                DataTable dtTop=dalTop.GetNewTopic("secId="+secId).Tables[0];
                if (dtTop.Rows.Count > 0)
                {
                    ((Label)dvSec.Rows[j].FindControl("lblAuthor")).Text = dtTop.Rows[0]["name"].ToString();
                    ((Label)dvSec.Rows[j].FindControl("lblTitle")).Text = dtTop.Rows[0]["title"].ToString();
                    ((Label)dvSec.Rows[j].FindControl("lblDate")).Text = dtTop.Rows[0]["sendTime"].ToString();
                }
            }
        }

    }


    //获得版主信息
    protected string getSecMst(string secId)
    {
        string retStr = "";
        DAL.bbsSecMaster master = new bbsSecMaster();
        DataTable dt = master.GetDetailedList("secId=" + secId).Tables[0];
        if (dt.Rows.Count > 0)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                retStr += dt.Rows[i]["name"].ToString() + ";";
            }
        }
        else
        {
            retStr = "[暂无]";
        }
        return retStr;
    }

    //为生成的每一行中内嵌的GridView2指定DataKeyNames(版块编号)
    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ((GridView)e.Row.Cells[0].FindControl("GridView2")).DataKeyNames = new string[] { "id" };
            }
        }
       
    }

    //为每个lblSec生成链接url
    protected void GridView2_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if(e.Row.RowState==DataControlRowState.Alternate||e.Row.RowState==DataControlRowState.Normal)
        {
            if(e.Row.RowType==DataControlRowType.DataRow)
            {
                string secId=((GridView)sender).DataKeys[e.Row.RowIndex].Value.ToString();
                ((LinkButton)e.Row.FindControl("lblSec")).PostBackUrl = "BBSListView.aspx?secId=" + secId;
            }
        }
    }
}
